using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AnalysisToolPanel : MonoBehaviour
{
    public InputField goUpRangeCheckDays;
    public InputField goUpRate;
    public Dropdown fromYearDropdown;

    public YijianfenghouUI yijianfenghouUI;
    
    public Text useTimeText;
    public Slider progress;
    public Text resultText;

    // Start is called before the first frame update
    void Start()
    {
        goUpRangeCheckDays.text = AppController.instance.StockDataAnalysis.goUpRangeCheckDays.ToString();
        goUpRate.text = (AppController.instance.StocksPicker.goUpRate*100).ToString();
        int thisYear = System.DateTime.Now.Year;

        var options = new List<Dropdown.OptionData>();
        for (int i =thisYear; i>= 2014; i--)
        {
            options.Add(new Dropdown.OptionData() { text = i.ToString() });
        }
        fromYearDropdown.AddOptions(options);
        fromYearDropdown.value = 4;

        AppController.instance.StockDataAnalysis.onProgress = this.OnProgress;
        AppController.instance.StockDataAnalysis.onEnd = this.OnExecuateEnd;
        AppController.instance.StocksPicker.onProgress = this.OnProgress;
        AppController.instance.StocksPicker.onEnd = this.OnExecuateEnd;
        AppController.instance.YijianfenghouAnalysis.onProgress = this.OnProgress;
        AppController.instance.YijianfenghouAnalysis.onEnd = this.OnExecuateEnd;
        AppController.instance.ShoubanAnalysisAnalysis.onProgress = this.OnProgress;
        AppController.instance.ShoubanAnalysisAnalysis.onEnd = this.OnExecuateEnd;
        AppController.instance.ErlianbanAnalysis.onProgress = this.OnProgress;
        AppController.instance.ErlianbanAnalysis.onEnd = this.OnExecuateEnd;
        AppController.instance.LizhuangliangAnalysis.onProgress = OnProgress;
        AppController.instance.LizhuangliangAnalysis.onEnd = OnExecuateEnd;
        
        useTimeText.gameObject.SetActive(false);
        progress.gameObject.SetActive(false);
        resultText.text = "";
    }

    public void OnGoUpRangeCheckDaysInput(string checkDaysStr)
    {
        if (int.TryParse(checkDaysStr, out int checkDays))
        {
            AppController.instance.StockDataAnalysis.goUpRangeCheckDays = checkDays;
            Debug.Log($"set go up ragnge checkDays:{checkDays}");
        }
    }

    public void OnGoUpRateInput(string rateStr)
    {
        if (float.TryParse(rateStr, out float rate))
        {
            rate /= 100;
            AppController.instance.StocksPicker.goUpRate = rate;
            Debug.Log($"set go up rate:{rate}");
        }
    }

    public void OnFromYearDropdownSelect(string value)
    {
        if(int.TryParse(value,out int year))
        {
            Debug.Log($"set data from year:{year}");
            AppController.instance.StockDataAnalysis.fromYear = year;
            AppController.instance.StocksPicker.dataFromYear = year;
        }
    }
    public void OnClickGetAllStocks()
    {
        Debug.Log("OnClickGetAllStocks");
        AppController.instance.DataFetchController.RequestAllStocks();
    }
    public void OnClickGetAllStocksSubData()
    {
        Debug.Log("OnClickGetAllStocksSubData");
        AppController.instance.DataFetchController.GetAllStockSubData();
    }
    public void OnClickCalAllStocksAnalysis()
    {
        Debug.Log("OnClickCalAllStocksAnalysis");
        AppController.instance.StockDataAnalysis.CalStockStatistic();
        OnBeginExecuate();
    }
    public void OnClickPickStocks()
    {
        Debug.Log("OnClickPickStocks");
        AppController.instance.StocksPicker.FindStocksWithZljlcAndLowHslAndGoUpTop();
        OnBeginExecuate();
    }
    public void OnClickPickYjfh()
    {
        Debug.Log("OnClickPickYjfh");
        AppController.instance.YijianfenghouAnalysis.PickAllYijianfenghou();
        OnBeginExecuate();
    }  
    public void OnClickPickShouban()
    {
        Debug.Log("OnClickPickShouban");
        AppController.instance.ShoubanAnalysisAnalysis.PickAllShouban();
        OnBeginExecuate();
    }   
    public void OnClickPickErlianban()
    {
        Debug.Log("OnClickPickErlianban");
        AppController.instance.ErlianbanAnalysis.PickAllErlianban();
        OnBeginExecuate();
    } 
    public void OnClickLizhuangliang()
    {
        Debug.Log("OnClickLizhuangliang");
        AppController.instance.LizhuangliangAnalysis.PickAllLizhuangliang();
        OnBeginExecuate();
    }
    public void OnClickYizizhuiyao()
    {
        Debug.Log("OnClickYizizhuiyao");
        AppController.instance.YizizhuiyaoAnalysis.PickAllYizizhuiyao();
        OnBeginExecuate();
    }
    private float m_beginTime;
    void OnBeginExecuate()
    {
        m_beginTime = Time.time;
        useTimeText.gameObject.SetActive(false);
        progress.gameObject.SetActive(true);
        resultText.text = "";
    }

    void OnProgress(float progress)
    {
        this.progress.value = progress;
    }

    public void OnExecuateEnd(string content)
    {
        float useTime = Time.time - m_beginTime;
        useTimeText.gameObject.SetActive(false);
        useTimeText.text = $"执行用时：{useTime}";
        Debug.Log($"执行结束，用时：{useTime}");
        progress.gameObject.SetActive(false);
        resultText.text = content;
    }

}
